package com.zs.studentmanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zs.studentmanager.entity.domain.GradeClass;
import com.zs.studentmanager.entity.domain.Student;
import com.zs.studentmanager.entity.vo.PageVo;
import com.zs.studentmanager.mapper.GradeClassMapper;
import com.zs.studentmanager.mapper.StudentMapper;
import com.zs.studentmanager.service.GradeClassService;
import org.springframework.stereotype.Service;

/**
* @author 周小白
* @description 针对表【s_grade_class(班级信息表)】的数据库操作Service实现
* @createDate 2024-01-19 19:58:37
*/
@Service
public class GradeClassServiceImpl extends ServiceImpl<GradeClassMapper, GradeClass>
    implements GradeClassService{

    private final GradeClassMapper gradeClassMapper;

    private final StudentMapper studentMapper;

    public GradeClassServiceImpl(GradeClassMapper gradeClassMapper, StudentMapper studentMapper) {
        this.gradeClassMapper = gradeClassMapper;
        this.studentMapper = studentMapper;
    }


    /**
     * 获取年级班级列表
     * @param pageNum 当前页码
     * @param pageSize 每页条数
     * @param searchValue 搜索关键字
     * @return PageVo<GradeClass>
     */
    @Override
    public PageVo<GradeClass> getGradeClassList(Integer pageNum, Integer pageSize, String searchValue) {
        Page<GradeClass> page = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<GradeClass> queryWrapper = new LambdaQueryWrapper<>();
        if (searchValue != null && !searchValue.isEmpty()) {
            queryWrapper.like(GradeClass::getName, searchValue);
        }
        queryWrapper.orderByDesc(GradeClass::getId);

        Page<GradeClass> gradeClassPage = gradeClassMapper.selectPage(page, queryWrapper);


        gradeClassPage.getRecords().forEach(item -> {
            LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Student::getGradeClassId, item.getId());
            item.setStudentCount(studentMapper.selectCount(wrapper));
        });

        PageVo<GradeClass> pageVo = new PageVo<>();
        pageVo.setList(gradeClassPage.getRecords());
        pageVo.setTotal(gradeClassPage.getTotal());
        pageVo.setPages(gradeClassPage.getPages());

        return pageVo;
    }
}




